package com.example.examoj.controller;

import com.example.examoj.common.Util;
import com.example.examoj.dao.User;
import com.example.examoj.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
public class UserController {
    @Autowired
    public ObjectMapper objectMapper;
    @Autowired
    public UserService userService;

    @PostMapping("/login")
    public void userLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setContentType("text/html; charset=utf8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (username == null || password == null || "".equals(username) || "".equals(password)) {
            String h3 = "<h3>输入格式错误</h3>";
            resp.getWriter().write(h3);
            return;
        }
        User user = userService.selectOneByName(username);
        if (user == null) {
            String h3 = "<h3>当前用户为空</h3>";
            resp.getWriter().write(h3);
            return;
        }
        if (!user.getUsername().equals(username) || !user.getPassword().equals(password)) {
            String h3 = "<h3>当前密码或者账号 错误！</h3>";
            resp.getWriter().write(h3);
            return;
        }
        HttpSession session = req.getSession(true);
        session.setAttribute("user", user);
        resp.sendRedirect("index.html");
    }

    @PostMapping("/register")
    protected void userRegister(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setContentType("text/html; charset=utf8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String admin = req.getParameter("admin");
        if (username == null || password == null || "".equals(username) || "".equals(password)) {
            String h3 = "<h3>输入格式错误</h3>";
            resp.getWriter().write(h3);
            return;
        }
        User user = userService.selectOneByName(username);
        if (user != null) {
            String h3 = "<h3>该用户名字已被使用！</h3>";
            resp.getWriter().write(h3);
            return;
        }
        User registerUser = new User();
        registerUser.setUsername(username);
        registerUser.setPassword(password);
        if (admin == null || "".equals(admin)) {
            registerUser.setIsAdmin(0);
            userService.insertUser(registerUser);
            resp.sendRedirect("login.html");
        } else {
            registerUser.setIsAdmin(Integer.parseInt(admin));
            userService.insertUser(registerUser);
            resp.sendRedirect("function.html");
        }
    }
    @GetMapping("/admin")
    public void isLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        User user = Util.checkUser(req);
        if (user == null) {
            resp.setStatus(403);
            String html = "<h3>当前用户未登录</h3>";
            resp.getWriter().write(html);
//                resp.sendRedirect("login.html");
            return;
        }
        String html = objectMapper.writeValueAsString(user);
        resp.getWriter().write(html);
    }
    @GetMapping("/logout")
    public void logoutUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            resp.setContentType("text/txt; charset=utf-8");
            String html = "<h3>当前尚未登录</h3>";
            resp.getWriter().write(html);
            return;
        }
        session.removeAttribute("user");
        resp.sendRedirect("login.html");
    }
    @PostMapping("/delete")
    public void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String username=req.getParameter("username");
        resp.setContentType("text/html; charset=utf8");
        User user=Util.checkUser(req);
        if (user==null){
            String html="<h3> 当前用户未登录 </h3>";
            resp.getWriter().write(html);
//            resp.sendRedirect("login.html");
            return;
        }
        if (user.getUsername().equals(username)){
            String html="<h3>不能删除自己</h3>";
            resp.getWriter().write(html);
            return;
        }

        User user1=userService.selectOneByName(username);
        if (user1==null){
            String html="<h3>删除用户不存在</h3>";
            resp.getWriter().write(html);
            return;
        }
        userService.deleteUser(user1.getId());
        resp.sendRedirect("function.html");
    }
}